<template>
  <a-modal
    title="添加物品"
    width="70%"
    :visible="visible"
    :confirmLoading="loading"
    okText="选择"
    @ok="handleChoose"
    @cancel="react"
  >
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="10">
          <a-col :md="4" :sm="24">
            <materialType-select v-model="queryParam.MaterialTypeId" placeholder="物品类别"></materialType-select>
          </a-col>
          <a-col :md="4" :sm="24">
            <a-form-item>
              <a-input v-model="queryParam.Keyword" placeholder="物品名称/编码" />
            </a-form-item>
          </a-col>
          <!--          <a-col :md="4" :sm="24">-->
          <!--            <a-form-item>-->
          <!--              <a-input v-model="queryParam.ProductCode" placeholder="成品编码" />-->
          <!--            </a-form-item>-->
          <!--          </a-col>-->
          <!-- <a-col :md="6" :sm="24">
            <a-form-item>
              <a-input v-model="queryParam.ContactName" placeholder="客户\供货单位名称或编码" />
            </a-form-item>
          </a-col> -->
          <a-col :md="6" :sm="24">
            <a-button
              type="primary"
              @click="
                () => {
                  this.pagination.current = 1
                  this.getDataList()
                }
              "
              >查询</a-button
            >
            <a-button style="margin-left: 8px" @click="reset">重置</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- getCheckboxProps: getCheckboxProps -->
    <a-table
      :columns="columns"
      :scroll="{ y: scroll }"
      :rowKey="row => row.Code"
      :dataSource="data"
      :pagination="pagination"
      :loading="loading"
      @change="handleTableChange"
      :rowSelection="{
        selectedRowKeys: selectedRowKeys,
        onChange: onSelectChange,
        type: type
      }"
      :bordered="true"
      size="small"
    >
      <template slot="dataIndex" slot-scope="text, record, index">
        <span>{{ (pagination.current - 1) * pagination.pageSize + index + 1 }}</span>
      </template></a-table
    >
  </a-modal>
</template>

<script>
import MaterialTypeSelect from '@/components/PB/MaterialTypeSelect'

export default {
  props: {
    type: { type: String, default: 'checkbox', required: false }
  },
  components: {
    MaterialTypeSelect
  },
  mounted() {
    // this.getDataList()
  },
  computed: {
    scroll() {
      // console.log()
      return Number(document.documentElement.clientHeight) * 0.45
    }
  },
  data() {
    return {
      columns: [
        {
          title: '序号',
          dataIndex: 'index',
          scopedSlots: { customRender: 'dataIndex' },
          width: '50px'
        },
        { title: '物品编号', dataIndex: 'Code', ellipsis: true },

        { title: '物品名称', dataIndex: 'Name' },
        {
          title: '物品规格',
          dataIndex: 'Spec',

          ellipsis: true
        },
        {
          title: '品牌',
          dataIndex: 'Brand',

          ellipsis: true
        },
        {
          title: '仓储单位',
          dataIndex: 'StorageName',
          ellipsis: true
        },
        {
          title: '采购单位',
          dataIndex: 'SaleName',
          ellipsis: true
        },
        {
          title: '是否批次管理',
          dataIndex: 'IsBatch',
          align: 'center',
          customRender: value => {
            if (value) {
              return '是'
            } else {
              return '否'
            }
          }
        },
        {
          title: '保质期',
          dataIndex: 'PeriodOfValidity',
          align: 'right'
        },
        {
          title: '保质期单位',
          dataIndex: 'PeriodOfValidityUnit'
        }
      ],
      data: [],
      qid: '',
      types: false,
      pagination: {
        current: 1,
        pageSize: 25,
        showTotal: (total, range) => `总数:${total} 当前:${range[0]}-${range[1]}`,
        pageSizeOptions: ['25', '50', '100'], //设置pageSize的可选值，页面啊可以通过下拉框进行选择
        showSizeChanger: true,
        onShowSizeChange: (current, pageSize) => (this.pageSize = pageSize)
      },
      filters: {},
      sorter: { field: 'Code', order: 'asc' },
      loading: false,

      queryParam: [],
      selectedRowKeys: [],
      selectedRows: [],
      visible: false
    }
  },
  methods: {
    reset() {
      if (this.types) {
        this.queryParam = []
      } else {
        this.queryParam = []
      }

      this.selectedRowKeys = []
      this.selectedRow = []
      this.pagination.current = 1
      this.getDataList()
    },
    getCheckboxProps(row) {
      return {
        props: {
          disabled: row.IsBecode == false
        }
      }
    },
    genTreeNode(parentId, isLeaf = false) {
      const random = Math.random()
        .toString(36)
        .substring(2, 6)
      return {
        id: random,
        pId: parentId,
        value: random,
        title: isLeaf ? 'Tree Node' : 'Expand to load',
        isLeaf
      }
    },
    react() {
      this.visible = false
      this.queryParam = {}
      this.pagination.current = 1
      this.pagination.pageSize = 25
    },
    handleTableChange(pagination, filters, sorter) {
      console.log(pagination)
      this.pagination = { ...pagination }
      this.filters = { ...filters }
      this.sorter = { ...sorter }
      this.selectedRowKeys = []
      this.selectedRow = []
      this.getDataList()
    },
    getDataList(id) {
      if (this.qid) {
        // this.queryParam.OrderId = this.qid
        this.queryParam = [{ Condition: 'MaterialTypeId', Keyword: this.MaterialTypeId }, {
          Condition: 'Name',
          Keyword: this.Name
        }, {
          Condition: 'ParentId',
          Keyword: this.qid
        }]
        this.loading = true
        // ParentId: '1457628868394356736'
        // var id = '1457628868394356736'
        this.$http
          .post('/PB/pb_purchasedetail/GetpurchasedetailList', {
            PageIndex: this.pagination.current,
            PageRows: this.pagination.pageSize,
            SortField: 'Code',
            SortType: 'desc',
            // Search: this.queryParam,
            Search: this.queryParam,
            ...this.filters
          })
          .then(resJson => {
            this.loading = false
            this.data = resJson.Data
            const pagination = { ...this.pagination }
            pagination.total = resJson.Total
            this.pagination = pagination
          })
      } else {
        this.loading = true
        this.$http
          .post('/PB/PB_Material/QueryDataList', {
            PageIndex: this.pagination.current,
            PageRows: this.pagination.pageSize,
            SortField: 'Id',
            SortType: 'desc',
            // Search: this.queryParam,
            Search: this.queryParam,

            ...this.filters
          })
          .then(resJson => {
            this.loading = false
            this.data = resJson.Data
            const pagination = { ...this.pagination }
            pagination.total = resJson.Total
            this.pagination = pagination
          })
      }
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    hasSelected() {
      return this.selectedRowKeys.length > 0
    },
    openChoose(id, types) {
      this.selectedRowKeys = []
      this.selectedRows = []
      this.types = types
      this.queryParam = []
      this.pagination.current = 1
      this.qid = id
      if (types) {
        this.queryParam.IsConsignment = true
      }
      this.getDataList()
      this.visible = true
    },
    handleChoose() {
      this.visible = false
      this.$emit('onChoose', this.selectedRows)
    }
  }
}
</script>
